package com.wxtt.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import com.fast.dev.frame.http.callback.StringHttpCallBack;
import com.fast.dev.frame.ui.Bind;
import com.fast.dev.frame.ui.ContentView;
import com.three.WeiboUtils;
import com.wxtt.android.R;
import com.wxtt.android.common.CommonActivity;
import com.wxtt.android.config.SpConfig;

/**
 * 说明：登录
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/3/4 15:34
 * <p/>
 * 版本：verson 1.0
 */
@ContentView(R.layout.activity_login)
public class LoginActivity extends CommonActivity{

    @Bind(id = R.id.ll_weibo,click = true)
    private LinearLayout ll_weibo;
    @Bind(id = R.id.ll_qq,click = true)
    private LinearLayout ll_qq;

    @Override
    protected boolean showToolbar() {
        return true;
    }

    @Override
    public void onInit(Bundle bundle) {
        super.onInit(bundle);
    }

    @Override
    public int setToolbarTitle() {
        return R.string.login_title;
    }

    @Override
    protected void onCustomToolbar(View view, Toolbar toolbar) {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void clickView(View v) {
        switch (v.getId()){
            case R.id.ll_weibo:
                WeiboUtils.get(this).login(new WeiboUtils.WeiboLoginListener() {
                    @Override
                    public void success() {
                        getWeiboUserInfo();
                    }

                    @Override
                    public void fail(String msg) {
                        shortToast(msg);
                    }

                    @Override
                    public void cancel() {
                        shortToast(R.string.cancel_login);
                    }
                });
                break;
            case R.id.ll_qq:
                shortMessage(ll_qq,"QQ登录");
                break;
        }
    }

    /**
     * 说明：获取微博用户信息
     */
    private void getWeiboUserInfo(){
        WeiboUtils.getUserInfo(new StringHttpCallBack() {
            @Override
            public void onSuccess(String result) {
                SpConfig.setLoginState(true);
                SpConfig.setUserInfo(result);
                finish();
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                SpConfig.setLoginState(false);
                shortToast(R.string.login_fail);
            }

        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        WeiboUtils.get(this).setAuthorizeCallBack(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        WeiboUtils.get(this).destroy();
        super.onDestroy();
    }
}
